﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class config_configration : System.Web.UI.Page
{
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadParameter(Getpath());
        }
    }
    //加载XML并显示在GridView里
    private void LoadParameter(string path)
    {
        DataSet dsRule = new DataSet();
        dsRule.ReadXml(path);

        DataTable dtRule = dsRule.Tables[0];

        gvParameter.DataSource = dtRule;
        gvParameter.DataBind();
    }

    string Getpath()
    {
        string xmlPath = "admin\\config\\config.xml";
        string s = Server.MapPath("~/");
        xmlPath = s + xmlPath;
        return xmlPath;
    }
    //恢复GridView正常状态
    private void ResetParameter()
    {
        gvParameter.EditIndex = -1;
        LoadParameter(Getpath());
    }
    #region


    ////恢复GridView正常状态
    //private void ResetParameter()
    //{
    //    gvParameter.EditIndex = -1;
    //    LoadParameter(ddlShapeType.SelectedValue);
    //}
    #region 


    ////恢复GridView正常状态
    //private void ResetParameter()
    //{
    //    gvParameter.EditIndex = -1;
    //    LoadParameter(ddlShapeType.SelectedValue);
    //}

    ////加载XML并显示在GridView里
    //private void LoadParameter()
    //{
    //    DataSet dsRule = new DataSet();
    //    dsRule.ReadXml(xmlPath);

    //    DataTable dtRule = dsRule.Tables[0];

    //    gvParameter.DataSource = dtRule;
    //    gvParameter.DataBind();
    //}

    ////新增事件
    //protected void lnbAddNew_Click(object sender, EventArgs e)
    //{
    //    if (ddlShapeType.SelectedValue.Length == 0)
    //    {
    //        lblMsg.Visible = true;
    //        lblMsg.Text = "Warning: Please select shape type !";
    //        return;
    //    }
    //    else if (gvParameter.Rows.Count == 0)
    //    {
    //        lblMsg.Visible = true;
    //        lblMsg.Text = "Warning: [" + ddlShapeType.SelectedValue + "] table not exists !";
    //        return;
    //    }

    //    DataSet ds = new DataSet();
    //    ds.ReadXml(xmlPath);
    //    DataTable dt = ds.Tables[ddlShapeType.SelectedValue];

    //    DataRow dr = dt.NewRow();
    //    dt.Rows.Add(dr);

    //    ds.WriteXml(xmlPath);   //将修改写入Table.xml

    //    ResetParameter();
    //}
    #endregion 
    ////编辑事件
    //protected void gvParameter_RowEditing(object sender, GridViewEditEventArgs e)
    //{
    //    gvParameter.EditIndex = e.NewEditIndex;
    //    LoadParameter(ddlShapeType.SelectedValue);

    //    GridViewRow row = gvParameter.Rows[e.NewEditIndex];

    //    //前面2列是Delete、Edit,故从第三列开始
    //    for (int i = 2; i < row.Cells.Count; i++)
    //    {
    //        TextBox tb = (TextBox)row.Cells[i].Controls[0];
    //        tb.Width = 50;
    //    }
    //}

    ////更新事件
    //protected void gvParameter_RowUpdating(object sender, GridViewUpdateEventArgs e)
    //{
    //    GridViewRow row = gvParameter.Rows[e.RowIndex]; //获得当前行

    //    int numCell = row.Cells.Count; //共几列单元格(包含Edit和Delete 2列)
    //    int currentRow = row.DataItemIndex; //对应DataSet对应的行索引

    //    DataSet ds = new DataSet();
    //    ds.ReadXml(xmlPath);
    //    DataRow dr;

    //    dr = ds.Tables[ddlShapeType.SelectedValue].Rows[row.DataItemIndex]; //找到对应与DataSet行

    //    string[] str = null;   //此数组定义表的列名

    //    switch (ddlShapeType.SelectedValue)
    //    {
    //        case "SOP":
    //            {
    //                str = new string[] { "PitchY", "T", "B", "WM", "WM1", "WP", "WP1", "R", "LM", "WCM", "WCP" };
    //                break;
    //            }
    //        case "DCHIP":
    //            {
    //                str = new string[] { "BodySize", "L", "W", "D", "T", "S", "R", "Wm", "Lm" };
    //                break;
    //            }
    //    }

    //    int j = 0;
    //    //从第3列开始,前面有2列是Edit和Delete
    //    for (int i = 2; i < numCell; i++)
    //    {
    //        string cText = ((TextBox)row.Cells[i].Controls[0]).Text;

    //        dr[str[j]] = cText;

    //        j++;
    //    }

    //    ds.WriteXml(xmlPath);   //将修改写入Table.xml

    //    ResetParameter();
    //}

    ////取消修改
    //protected void gvParameter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    //{
    //    ResetParameter();
    //}

    ////删除事件
    //protected void gvParameter_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    GridViewRow row = gvParameter.Rows[e.RowIndex];

    //    int curr = row.RowIndex;
    //    DataSet ds = new DataSet();
    //    ds.ReadXml(xmlPath);

    //    DataRow dr = ds.Tables[ddlShapeType.SelectedValue].Rows[curr];
    //    dr.Delete();
    //    ds.WriteXml(xmlPath);

    //    ResetParameter();
    //}

    #endregion
    protected void btnadd_Click(object sender, EventArgs e)
    {
        

        DataSet ds = new DataSet();
        ds.ReadXml(Getpath());
        DataTable dt = ds.Tables[0];

        DataRow dr = dt.NewRow();
        dr["标题"] = txttitle.Text;
        dr["地址"] = txturl.Text;
        dt.Rows.Add(dr);

        ds.WriteXml(Getpath());   //将修改写入Table.xml

        ResetParameter();
    }
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvParameter_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Getpath());
        DataTable dt = ds.Tables[0];

        TextBox txt=(TextBox)gvParameter.Rows[e.RowIndex].Cells[0].Controls[0];
        dt.Rows[e.RowIndex][0] = txt.Text;

        TextBox txturl = (TextBox)gvParameter.Rows[e.RowIndex].Cells[1].Controls[0];
        dt.Rows[e.RowIndex][1] = txturl.Text;
 

         
        ds.WriteXml(Getpath());   //将修改写入Table.xml

        ResetParameter();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvParameter_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = gvParameter.Rows[e.RowIndex];

        int curr = row.RowIndex;
        DataSet ds = new DataSet();
        ds.ReadXml(Getpath());

        DataRow dr = ds.Tables[0].Rows[curr];
        dr.Delete();
        ds.WriteXml(Getpath());

        ResetParameter();
    }

    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvParameter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        ResetParameter();
    }
    protected void gvParameter_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvParameter.EditIndex = e.NewEditIndex;
        LoadParameter(Getpath());
    }
}
